//
//  CollectionViewController.swift
//  myhub
//
//  Created by x on 2024/3/6.
//

import Foundation

open class CollectionViewController: ViewController {

    // 可以重写为计算属性
    open lazy var collectionView: CollectionView = {
        let view = CollectionView()
        view.emptyDataSetSource = self
        view.emptyDataSetDelegate = self
        return view
    }()
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    open var clearsSelectionOnViewWillAppear = true

    open override func viewDidLoad() {
        super.viewDidLoad()
    }
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if clearsSelectionOnViewWillAppear == true {
            deselectSelectedItems()
        }
    }

    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    open override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }

    
    open override func makeUI() {
        super.makeUI()

        stackView.spacing = 0
        stackView.insertSubview(collectionView, at: 0)
    }
}

extension CollectionViewController {
    open func deselectSelectedItems() {
        if let selectedIndexPaths = collectionView.indexPathsForSelectedItems {
            selectedIndexPaths.forEach { indexPath in
                collectionView.deselectItem(at: indexPath, animated: false)
            }
        }
    }
}
